package com.examstack.management.controller.action.admin;

import com.examstack.common.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.examstack.common.domain.exam.Message;
import com.examstack.common.domain.training.Training;
import com.examstack.common.domain.training.TrainingSection;
import com.examstack.management.security.UserInfo;
import com.examstack.management.service.TrainingService;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class TrainingActionAdmin {
	@Autowired
	private TrainingService trainingService;

	@RequestMapping(value = "admin/training/add-training", method = RequestMethod.POST)
	public @ResponseBody Message addTraining(@RequestBody Training training) {

		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		
		Message msg = new Message();
		try {
			training.setStateTime(new Date());
			training.setCreatorId(userInfo.getUserid());
			trainingService.addTraining(training);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			msg.setResult(e.getClass().getName());
		}
		return msg;
	}
	
	@RequestMapping(value = "admin/training/add-training-section", method = RequestMethod.POST)
	public @ResponseBody Message addTrainingSection(@RequestBody TrainingSection section){

		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		
		Message msg = new Message();
		try {
			section.setFileMd5(section.getFilePath()
					.substring(section.getFilePath().lastIndexOf("\\")+1,
							section.getFilePath().indexOf(".")));
			section.setUserId(userInfo.getUserid());
			trainingService.addTrainingSection(section);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			msg.setResult(e.getClass().getName());
		}
		return msg;
	}

	/**
	 * 删除一个培训课程
	 * @param trainingId
	 * @return
	 */
	@RequestMapping(value = "/admin/common/training-delete-{trainingId}", method = RequestMethod.GET)
	public @ResponseBody Message deleteTraining(@PathVariable("trainingId") int trainingId){

		Message msg = new Message();
		try {
			//判断该培训课程是否存在章节
			Page<TrainingSection> page=new Page<TrainingSection>();
			page.setPageNo(1);
			page.setPageSize(5);
			List<TrainingSection> listSection=trainingService.getTrainingSectionByTrainingId(trainingId,-1,page);
			//不存在则进行删除
			if(listSection.size()>0){
				msg.setResult("该培训课程存在章节内容，无法删除！");
				return msg;
			}
			trainingService.deleteTraining(trainingId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			msg.setResult(e.getClass().getName());
		}
		return msg;
	}

	/**
	 * 删除一个培训章节
	 * @param sectionId
	 * @return
	 */
	@RequestMapping(value = "/admin/common/training-section-delete-{sectionId}", method = RequestMethod.GET)
	public @ResponseBody Message deleteSection(@PathVariable("sectionId") int sectionId){

		Message msg = new Message();
		try {
			//删除该章节资料
			Page<TrainingSection> page=new Page<TrainingSection>();
			page.setPageNo(1);
			page.setPageSize(5);
			List<TrainingSection> listSection=trainingService.getTrainingSectionById(sectionId,-1,page);
			String filepath = System.getProperty("catalina.home") + ",webapps,".replace(',', File.separatorChar);
			File existsFile = new File(filepath+listSection.get(0).getFilePath());
			if(existsFile.exists()){
				existsFile.delete();
			}
			trainingService.deleteTrainingSection(sectionId,-1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			msg.setResult(e.getClass().getName());
		}
		return msg;
	}
}
